﻿using GalaSoft.MvvmLight;
using Platformer;
using System;
using System.Windows.Media;
using System.Windows;

namespace SilverXNA.ViewModel
{
	/// <summary>
	/// This class contains properties that a View can data bind to.
	/// <para>
	/// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
	/// </para>
	/// <para>
	/// You can also use Blend to data bind with the tool's support.
	/// </para>
	/// <para>
	/// See http://www.galasoft.ch/mvvm/getstarted
	/// </para>
	/// </summary>
	public class GameViewModel : ViewModelBase
	{
		SolidColorBrush YellowBrush = new SolidColorBrush(Colors.Yellow);
		SolidColorBrush RedBrush = new SolidColorBrush(Colors.Red);

		/// <summary>
		/// Initializes a new instance of the GameViewModel class.
		/// </summary>
		public GameViewModel()
		{
			////if (IsInDesignMode)
			////{
			////    // Code runs in Blend --> create design time data.
			////}
			////else
			////{
			////    // Code runs "for real": Connect to service, etc...
			////}
		}

		////public override void Cleanup()
		////{
		////    // Clean own resources if needed

		////    base.Cleanup();
		////}

		/// <summary>
		/// The <see cref="GameLevel" /> property's name.
		/// </summary>
		public const string GameLevelPropertyName = "GameLevel";

		private Level _gameLevel = null;

		/// <summary>
		/// Gets the GameLevel property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public Level GameLevel
		{
			get
			{
				return _gameLevel;
			}

			set
			{
				if (_gameLevel == value)
				{
					return;
				}

				var oldValue = _gameLevel;
				_gameLevel = value;

				// Update bindings, no broadcast
				RaisePropertyChanged(GameLevelPropertyName);
			}
		}

		/// <summary>
		/// The <see cref="WarningTime" /> property's name.
		/// </summary>
		public const string WarningTimePropertyName = "WarningTime";

		private TimeSpan _warningTime = TimeSpan.FromSeconds(30);

		/// <summary>
		/// Gets the WarningTime property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public TimeSpan WarningTime
		{
			get
			{
				return _warningTime;
			}

			set
			{
				if (_warningTime == value)
				{
					return;
				}

				var oldValue = _warningTime;
				_warningTime = value;

				// Update bindings, no broadcast
				RaisePropertyChanged(WarningTimePropertyName);
			}
		}

		/// <summary>
		/// The <see cref="GameTime" /> property's name.
		/// </summary>
		public const string GameTimePropertyName = "GameTime";

		private TimeSpan _gameTime = new TimeSpan();

		/// <summary>
		/// Gets the GameTime property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public TimeSpan GameTime
		{
			get
			{
				return _gameTime;
			}

			set
			{
				if (_gameTime == value)
				{
					return;
				}

				var oldValue = _gameTime;
				_gameTime = value;

				// Update bindings, no broadcast
				RaisePropertyChanged(GameTimePropertyName);
				RaisePropertyChanged(GameTimeStringPropertyName);

				if (GameLevel.TimeRemaining > WarningTime ||
					GameLevel.ReachedExit ||
					(int)GameLevel.TimeRemaining.TotalSeconds % 2 == 0)
				{
					GameTimeDisplayColor = YellowBrush;
				}
				else
				{
					GameTimeDisplayColor = RedBrush;
				}

				if (GameLevel.TimeRemaining == TimeSpan.Zero)
				{
					if (GameLevel.ReachedExit)
					{
                        CurrentState = "DisplayWin";
					}
					else
					{
                        CurrentState = "DisplayLose";
					}
				}
				else if (!GameLevel.Player.IsAlive)
				{
                    CurrentState = "DisplayDied";
				}
				else
				{
                    CurrentState = "GameRunning";
				}
			}
		}

		/// <summary>
		/// The <see cref="GameScore" /> property's name.
		/// </summary>
		public const string GameScorePropertyName = "GameScore";

		private int _gameScore = 0;

		/// <summary>
		/// Gets the GameScore property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public int GameScore
		{
			get
			{
				return _gameScore;
			}

			set
			{
				if (_gameScore == value)
				{
					return;
				}

				var oldValue = _gameScore;
				_gameScore = value;

				// Update bindings, no broadcast
				RaisePropertyChanged(GameScorePropertyName);
			}
		}

		/// <summary>
		/// The <see cref="GameTimeString" /> property's name.
		/// </summary>
		public const string GameTimeStringPropertyName = "GameTimeString";

		/// <summary>
		/// Gets the GameTimeString property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public String GameTimeString
		{
			get
			{
				return _gameTime.Minutes.ToString("00") + ":" + _gameTime.Seconds.ToString("00");
			}
		}


		/// <summary>
		/// The <see cref="GameTimeDisplayColor" /> property's name.
		/// </summary>
		public const string GameTimeDisplayColorPropertyName = "GameTimeDisplayColor";

		private SolidColorBrush _gameTimeDisplayColor = new SolidColorBrush(Colors.Yellow);

		/// <summary>
		/// Gets the GameTimeDisplayColor property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public SolidColorBrush GameTimeDisplayColor
		{
			get
			{
				return _gameTimeDisplayColor;
			}

			set
			{
				if (_gameTimeDisplayColor == value)
				{
					return;
				}

				var oldValue = _gameTimeDisplayColor;
				_gameTimeDisplayColor = value;

				// Update bindings, no broadcast
				RaisePropertyChanged(GameTimeDisplayColorPropertyName);
			}
		}

		/// <summary>
		/// The <see cref="WinOverlayVisibility" /> property's name.
		/// </summary>
		public const string WinOverlayVisibilityPropertyName = "WinOverlayVisibility";

		private Visibility _winOverlayVisibility = Visibility.Collapsed;

		/// <summary>
		/// Gets the WinOverlayVisibility property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public Visibility WinOverlayVisibility
		{
			get
			{
				return _winOverlayVisibility;
			}

			set
			{
				if (_winOverlayVisibility == value)
				{
					return;
				}

				var oldValue = _winOverlayVisibility;
				_winOverlayVisibility = value;

				// Update bindings, no broadcast
				RaisePropertyChanged(WinOverlayVisibilityPropertyName);
			}
		}

		/// <summary>
		/// The <see cref="WinOverlayVisibility" /> property's name.
		/// </summary>
		public const string LoseOverlayVisibilityPropertyName = "LoseOverlayVisibility";

		private Visibility _loseOverlayVisibility = Visibility.Collapsed;

		/// <summary>
		/// Gets the WinOverlayVisibility property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public Visibility LoseOverlayVisibility
		{
			get
			{
				return _loseOverlayVisibility;
			}

			set
			{
				if (_loseOverlayVisibility == value)
				{
					return;
				}

				var oldValue = _winOverlayVisibility;
				_loseOverlayVisibility = value;

				// Update bindings, no broadcast
				RaisePropertyChanged(LoseOverlayVisibilityPropertyName);
			}
		}

		/// <summary>
		/// The <see cref="WinOverlayVisibility" /> property's name.
		/// </summary>
		public const string DiedOverlayVisibilityPropertyName = "DiedOverlayVisibility";

		private Visibility _diedOverlayVisibility = Visibility.Collapsed;

		/// <summary>
		/// Gets the WinOverlayVisibility property.
		/// TODO Update documentation:
		/// Changes to that property's value raise the PropertyChanged event. 
		/// This property's value is broadcasted by the Messenger's default instance when it changes.
		/// </summary>
		public Visibility DiedOverlayVisibility
		{
			get
			{
				return _diedOverlayVisibility;
			}

			set
			{
				if (_diedOverlayVisibility == value)
				{
					return;
				}

				var oldValue = _diedOverlayVisibility;
				_winOverlayVisibility = value;

				// Update bindings, no broadcast
				RaisePropertyChanged(DiedOverlayVisibilityPropertyName);
			}
		}

                /// <summary>
        /// The <see cref="CurrentState" /> property's name.
        /// </summary>
        public const string CurrentStatePropertyName = "CurrentState";

        private string _CurrentState = "GameRunning";

        /// <summary>
        /// Gets the CurrentState property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public string CurrentState
        {
            get
            {
                return _CurrentState;
            }

            set
            {
                if (_CurrentState == value)
                {
                    return;
                }

                var oldValue = _CurrentState;
                _CurrentState = value;

                // Update bindings, no broadcast
                RaisePropertyChanged(CurrentStatePropertyName);
            }
        }
	}
}